#!/bin/sh

: ${NTP_SERVER:='0.boot2docker.pool.ntp.org 1.boot2docker.pool.ntp.org 2.boot2docker.pool.ntp.org 3.boot2docker.pool.ntp.org'}

first=
args=
for server in $NTP_SERVER; do
	[ -n "$first" ] || first="$server"
	args="$args -p $server"
done

# if NTP_SERVER is set to ' ', ntp code can be skipped
if [ -z "$first" ]; then
	echo 'NTP disabled' > /var/lib/boot2docker/log/ntpd.log
	exit
fi

# Wait on the network
count=10
while ! ping -c 1 $first > /dev/null 2>&1; do
	sleep 1
	count=$(( count - 1 ))
	if [ $count -lt 1 ]; then
		break
	fi
done

ntpd -d -n $args >> /var/lib/boot2docker/log/ntpd.log 2>&1 &
